﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace StarForce {
    [RequireComponent(typeof(PlayerHand))]
    public class PlyerHandSorter : MonoBehaviour {
        private CardPile PlayerHand { get; set; }
        private const int offsetZ = -1;

        private void Awake() {
            PlayerHand = GetComponent<PlayerHand>();
            PlayerHand.OnPileChange += Sort;
        }

        public void Sort(Card[] cards) {
            if (cards == null) {
                throw new ArgumentNullException("Can't sort a null card list!");
            }

            int layerZ = 0;
            foreach (var card in cards) {
                Vector3 localCardPosition = card.transform.localPosition;
                localCardPosition.z = layerZ;
                card.transform.localPosition = localCardPosition;
                layerZ += offsetZ;
            }
        }
    }
}
